package com.ruoyi.system.service.impl;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.alibaba.fastjson2.JSONArray;
import com.alibaba.fastjson2.JSONObject;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.system.domain.*;
import com.ruoyi.system.domain.vo.ByBsVo;
import com.ruoyi.system.mapper.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.system.service.IByBsService;

/**
 * 帮手Service业务层处理
 *
 * @author ruoyi
 * @date 2023-05-16
 */
@Service
public class ByBsServiceImpl implements IByBsService {
    @Autowired
    private ByBsMapper byBsMapper;

    @Autowired
    private ByCodeMsgMapper codeMsgMapper;

    @Autowired
    private DateUtilSqlMapper dateUtilSqlMapper;


    @Autowired
    private ByBsTimeMapper bsTimeMapper;

    @Autowired
    private BySubjectClassifyMapper subjectClassifyMapper;


    @Autowired
    private ByBsSubjectMapper bsSubjectMapper;

    @Autowired
    private BySchoolMapper schoolMapper;

    @Autowired
    private ByParamMapper paramMapper;

    @Autowired
    private ByOrderMapper orderMapper;

    @Autowired
    private ByJzYhqMapper byJzYhqMapper;


    @Override
    public boolean saveOrDelBsSubject(String bsid, String subjectid) {
        List<JSONObject> list = bsSubjectMapper.getEntityBySubjectIdAndBsId(subjectid, bsid);
        if(list.size() > 0){
            bsSubjectMapper.delEntityBySubjectIdAndBsId(subjectid,bsid);
            return true;
        }else{
            ByBsSubject entity = new ByBsSubject();
            entity.setBsid(bsid);
            entity.setSubject(subjectid);
            bsSubjectMapper.insertByBsSubject(entity);
            return true;
        }
    }

    @Override
    public List<ByBsSubject> getSubjectIdForByid(String bsid) {
        return bsSubjectMapper.getSubjectIdForByid(bsid);
    }

    @Override
    public Object isShowBsOrderTimeDetail(JSONObject json) {
        String date = json.getString("date");
        String bsid = json.getString("bsid");
        List<ByOrder> list = orderMapper.getBsOrderListByBsIdAndDate(bsid, date);
        JSONObject result = new JSONObject();
        if(list.size() > 0){
            String jg = orderMapper.BsDateTimeDetailByBsidAndDate(bsid, date);
            result.put("status","1");
            result.put("msg",jg);
            return result;
        }else{
            result.put("status","0");
            result.put("msg","");
            return result;
        }
    }

    @Override
    public Object isGetBsStatus(JSONObject json) {
        String wxid = json.getString("wxid");
        List<ByBs> list = byBsMapper.getBsListByWxid(wxid);
        JSONObject result = new JSONObject();
        if(list.size() > 0){
            result.put("status","1");
            result.put("msg","存在审核中的帮手信息......");
            return result;
        }else{
            result.put("status","0");
            result.put("msg","不存在审核中的帮手信息......");
            return result;
        }
    }

    @Override
    public Object getNowDateList() {
        Map map = new HashMap();

        List<JSONObject> dateList = new ArrayList<>();
        for(int i = 0;i < 7;i++){
            String date = dateUtilSqlMapper.getDateAddDay(i);
            JSONObject json = new JSONObject();
            json.put("date",date);
            json.put("status","0");
            json.put("id",date);
            dateList.add(json);
        }

        List<JSONObject> timelist = paramMapper.getParamStatusByPid("2");

        map.put("dateList",dateList);
        map.put("timelist",timelist);

        return map;
    }

    @Override
    public JSONObject getBsByWxid(JSONObject json) {
        String wxid = json.getString("wxid");

        return byBsMapper.getBsByWxid(wxid);
    }

    @Override
    public void upBsStatus(JSONObject json) {
        String id = json.getString("id");
        String status = json.getString("status");
        byBsMapper.upBsStatus(id,status);
    }

    @Override
    public void mangSubjectAndBsid(JSONObject json) {
        String subjectid = json.getString("subjectid");
        String bsid = json.getString("bsid");
        // 进行查询获取一下当前帮手是否开设当前科目
        List<JSONObject> list = bsSubjectMapper.getEntityBySubjectIdAndBsId(subjectid, bsid);
        if(list.size() > 0){// 进行删除
            bsSubjectMapper.delEntityBySubjectIdAndBsId(subjectid, bsid);
        }else{// 进行添加
            ByBsSubject entity = new ByBsSubject();
            entity.setBsid(bsid);
            entity.setSubject(subjectid);
            bsSubjectMapper.insertByBsSubject(entity);
        }

    }

    @Override
    public List<JSONObject> getBsSubject(JSONObject json) {
        String id = json.getString("id");
        return subjectClassifyMapper.getAllSubjectNoTopByBsId(id);
    }

    @Override
    public Map<String,Object> bsOrderTime(JSONObject json) {
        String id = json.getString("id");// 获取帮手的ID
        // 获取当天的日期 获取当前时间，如果超过16点需要预约明天的
//        String hr = dateUtilSqlMapper.getNowHr();
        String date = json.getString("date");// dateUtilSqlMapper.getNowDate();
        /*if(Integer.parseInt(hr) >= 18){// 只能预约第二天的课程
            date = dateUtilSqlMapper.getNextDate();
        }*/

        List<ByBsTime> list = bsTimeMapper.getBsTimeForByIdAndDate(id, date);
        if(list.size() > 0){
            ByBsTime entity = list.get(0);
            Map<String,Object> map = new HashMap<>();
            map.put("list",getBsTimeList(entity));
            map.put("date",date);
            return map;
        }


        return null;
    }

    // 获取当天的日期 获取当前时间，如果超过16点需要预约明天的
    /*int hr = Integer.parseInt(dateUtilSqlMapper.getNowHr()); //获取当前时间-小时
        entity.getEight();
        if(hr >= 18){
            for(int i = 8;i <= 18;i++){
                String status = getBsTimeStatus(entity, i);
                if(status.equals("1")){
                    JSONObject json = new JSONObject();
                    String time = i+":00";
                    json.put("time",time);
                    json.put("status","0");
                    list.add(json);
                }
            }
        }else{
            for(int i = 8;i <= 18;i++){
                String status = getBsTimeStatus(entity, i);
                if(status.equals("1")){
                    JSONObject json = new JSONObject();
                    String time = i+":00";
                    json.put("time",time);
                    json.put("status","0");
                    list.add(json);
                }
            }
        }*/
    private List<JSONObject> getBsTimeList(ByBsTime entity){
        List<JSONObject> list = new ArrayList<>();
        // 进行判断当前日期的时间，小于当前时间，不进行展示
        String date = entity.getDate();
        String nowDate = dateUtilSqlMapper.getNowDate();
        if(date.equals(nowDate)){
            int hr = Integer.parseInt(dateUtilSqlMapper.getNowHr());// 获取当前小时数
            for(int i = 8;i <= 19;i++){
                if(i >= hr){
                    String status = getBsTimeStatus(entity, i);
                    if(status.equals("1")){
                        JSONObject json = new JSONObject();
                        String time = i+":00";
                        if (i == 19){
                            time = "18:30";
                        }
                        json.put("time",time);
                        json.put("status","0");
                        list.add(json);
                    }
                }
            }
        }else{
            for(int i = 8;i <= 19;i++){
                String status = getBsTimeStatus(entity, i);
                if(status.equals("1")){
                    JSONObject json = new JSONObject();
                    String time = i+":00";
                    if (i == 19){
                        time = "18:30";
                    }
                    json.put("time",time);
                    json.put("status","0");
                    list.add(json);
                }
            }
        }



        return list;
    }

    private String getBsTimeStatus(ByBsTime entity,int hr){
        if(hr == 8){
            return entity.getEight();
        }
        if(hr == 9){
            return entity.getNine();
        }
        if(hr == 10){
            return entity.getTen();
        }
        if(hr == 11){
            return entity.getEleven();
        }
        if(hr == 12){
            return entity.getTwelve();
        }
        if(hr == 13){
            return entity.getThirteen();
        }
        if(hr == 14){
            return entity.getFourteen();
        }
        if(hr == 15){
            return entity.getFifteen();
        }
        if(hr == 16){
            return entity.getSixteen();
        }
        if(hr == 17){
            return entity.getSeventeen();
        }
        if(hr == 18){
            return entity.getEighteen();
        }
        if (hr == 19){
            return entity.getHalfpasteighteen();
        }
        return null;
    }


    @Override
    public List<ByBs> pageBangshouIndexShow() {
        return byBsMapper.pageBangshouIndexShow();
    }

    @Override
    public JSONObject bsDetailById(String id) {
        return byBsMapper.bsDetailById(id);
    }

    @Override
    public List<ByBs> pageBangshou(JSONObject json) {
        int page = Integer.parseInt(json.getString("page"));
        String schlist = json.getString("schlist");
        String date = json.getString("date");
        String time = json.getString("time");

        String[] schoolid = null;
        if (!StringUtils.isEmpty(schlist)) {
            JSONArray jalist = JSONArray.parse(schlist);
            schoolid = new String[jalist.size()];
            int i = 0;
            for(Object json2:jalist){
                JSONObject json3 = (JSONObject) json2;
                schoolid[i]= json3.getString("id");
                i++;
            }
        }
        page = 10*(page-1);


        String subjectid = json.getString("subjectid");
        ByBsVo entity = new ByBsVo();
        entity.setPage(page);
        entity.setSubjectid(subjectid);
        entity.setSchoolid(schoolid);
        entity.setDate(date);
        entity.setTime(time);


        return byBsMapper.searchWxPage(entity);
    }

    @Override
    public JSONObject queryByWxid(String wxid) {
        JSONObject json = new JSONObject();
        ByBs entity = byBsMapper.queryByWxid(wxid);
//        return byBsMapper.queryByWxid(wxid);
        if(entity != null){
            json.put("code","400");
        }else{
            json.put("code","200");
        }
        return json;
    }

    @Override
    public ByBs queryBsEntityByWxid(String wxid) {
        return byBsMapper.queryByWxid(wxid);
    }


    @Override
    public JSONObject managSave(ByBs entity) {
        JSONObject json = new JSONObject();
        /***********************************************/
        /*String cardno = entity.getCardno();//获取证件号码，判断当前人员是否存在
        List<ByBs> list = byBsMapper.queryCountByCardNo(cardno);
        if(list.size() > 0){
            json.put("code","400");
            json.put("msg","当前帮手已经存在！");
            return json;
        }*/
//        String id = entity.getId() + "";
        if(entity.getId() != null){
            updateByBs(entity);
            json.put("code","200");
            return json;
        }


        //进行验证短信问题
        String mobile = entity.getMobile();
        String code = entity.getCode();
        List<ByCodeMsg> listMsg = codeMsgMapper.queryByCodeAndMobile(mobile, code);
        if(listMsg.size() == 0){
            json.put("code","400");
            json.put("msg","短信验证码输入错误，请检查！");
            return json;
        }
        entity.setAddtime(dateUtilSqlMapper.getNowDateTime());
//        entity.setUpdateTime(dateUtilSqlMapper.getNowDateTime());
        insertByBs(entity);
        json.put("code","200");
        return json;
    }

    /**
     * 查询帮手
     *
     * @param id 帮手主键
     * @return 帮手
     */
    @Override
    public ByBs selectByBsById(Long id)
    {
        return byBsMapper.selectByBsById(id);
    }

    /**
     * 查询帮手列表
     *
     * @param byBs 帮手
     * @return 帮手
     */
    @Override
    public List<ByBs> selectByBsList(ByBs byBs)
    {
        return byBsMapper.selectByBsList(byBs);
    }

    /**
     * 新增帮手
     *
     * @param byBs 帮手
     * @return 结果
     */
    @Override
    public int insertByBs(ByBs byBs)
    {
        return byBsMapper.insertByBs(byBs);
    }

    /**
     * 修改帮手
     *
     * @param byBs 帮手
     * @return 结果 
     */
    @Override
    public int updateByBs(ByBs byBs){
        int schoolid = byBs.getSchoolId();
        BySchool school = schoolMapper.selectBySchoolById(Long.parseLong(schoolid + ""));
        byBs.setSchool(school.getName());
        JSONObject entity = paramMapper.getParamJSONObjectById(byBs.getFdid());//.getParamByPid(byBs.getFdid());
        byBs.setFdval(entity.getString("val"));

        return byBsMapper.updateByBs(byBs);
    }

    /**
     * 批量删除帮手
     *
     * @param ids 需要删除的帮手主键
     * @return 结果
     */
    @Override
    public int deleteByBsByIds(Long[] ids)
    {
        return byBsMapper.deleteByBsByIds(ids);
    }

    /**
     * 删除帮手信息
     *
     * @param id 帮手主键
     * @return 结果
     */
    @Override
    public int deleteByBsById(Long id)
    {
        return byBsMapper.deleteByBsById(id);
    }

    @Override
    public ByJzYhq getVip(JSONObject json) {
        String wxid = json.getString("wxid");
        return byJzYhqMapper.getVip(wxid);
    }
}
